package cn.wawi.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @description 加密/解密  mds/sha加密
 * @author 龚亮
 * @datetime 2014-10-13 12:01:08
 */
public class StringEscapeUtil extends StringEscapeUtils{
	
	private static final String DEFAULT_URL_ENCODING = "UTF-8";

	/**
	 * 二次加密
	 * @param inputText
	 * @return
	 */
	public static String md5AndSha(String inputText) {
		return sha(md5(inputText));
	}

	/**
	 * md5加密
	 * @param inputText
	 * @return
	 */
	public static String md5(String inputText) {
		return encrypt(inputText, "md5");
	}

	/**
	 * sha加密
	 * @param inputText
	 * @return
	 */
	public static String sha(String inputText) {
		return encrypt(inputText, "sha-1");
	}

	/**
	 * md5或者sha-1加密
	 * @param inputText   要加密的内容
	 * @param algorithmName  加密算法名称：md5或者sha-1，不区分大小写
	 * @return
	 */
	private static String encrypt(String inputText, String algorithmName) {
		if (inputText == null || "".equals(inputText.trim())) {
			throw new IllegalArgumentException("请输入要加密的内容");
		}
		if (algorithmName == null || "".equals(algorithmName.trim())) {
			algorithmName = "md5";
		}
		String encryptText = null;
		try {
			MessageDigest m = MessageDigest.getInstance(algorithmName);
			m.update(inputText.getBytes("UTF8"));
			byte s[] = m.digest();
			// m.digest(inputText.getBytes("UTF8"));
			return hex(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return encryptText;
	}

	/**
	 * 返回十六进制字符串
	 * @param arr
	 * @return
	 */
	private static String hex(byte[] arr) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arr.length; ++i) {
			sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3));
		}
		return sb.toString();
	}
	/**
	 * 可逆的加密算法 加密解密算法 执行一次加密，两次解密
	 */
	public static String convertMD5(String inStr) {

		char[] a = inStr.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 'v');
		}
		String s = new String(a);
		return s;
	}
	
	/**
	 * Base64编码
	 */
	public static String base64encode(String message) {
		BASE64Encoder base64Encoder = new BASE64Encoder();
		return base64Encoder.encode(message.getBytes());
	}

	/**
	 * Base64解码
	 * @throws IOException
	 */
	public static String base64decode(String message) throws IOException {
		BASE64Decoder base64Decoder = new BASE64Decoder();
		byte[] buf = base64Decoder.decodeBuffer(message);
		return new String(buf);
	}
    public static String escapeSql(String str) {
        if (str == null) {
            return null;
        }
        return StringUtils.replace(str, "'", "''");
    }
	/**
	 * Hex编码.
	 */
	public static String encodeHex(byte[] input) {
		return new String(Hex.encodeHex(input));
	}

	/**
	 * Hex解码.
	 */
	public static byte[] decodeHex(String input) {
		try {
			return Hex.decodeHex(input.toCharArray());
		} catch (DecoderException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * URL 编码, Encode默认为UTF-8. 
	 */
	public static String urlEncode(String part) {
		try {
			return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * URL 解码, Encode默认为UTF-8. 
	 */
	public static String urlDecode(String part) {

		try {
			return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static void main(String[] args) {
		System.out.println(md5(hex("123456".getBytes())));
		System.out.println(hex(md5("123456").getBytes()));
		// md5加密测试
		String md5_1 = md5("123456");
		System.out.println(md5_1);
		// sha加密测试
		String sha_1 = sha("123456");
		System.out.println(sha_1);
		String md5_sha = md5AndSha("123456");
		System.out.println(md5_sha);
		
		/********base64加密**************/
		// 迅雷地址获得 --- 使用Base64
		String url = "www.baidu.cn/爱情公寓1.rmvb";
		// 在地址前后添加 AA和ZZ
		url = "AA" + url + "ZZ";
		// 进行base64编码
		url = base64encode(url);

		// 在路径前 thunder://
		url = "thunder://" + url;
		System.out.println(url);
	}
}
